package com.hemerasolutions.v2trainer.component.search;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hemerasolutions.v2trainer.component.BaseComponent;
import com.hemerasolutions.v2trainer.component.interfaces.detail.ExerciseGroupDetailsCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.search.ExerciseGroupSearchCompInterface;
import com.hemerasolutions.v2trainer.delegate.interfaces.ExerciseGroupSearchDelegateInterface;
import com.hemerasolutions.v2trainer.model.exerciseGroup.ExerciseGroup;
import com.hemerasolutions.v2trainer.model.exerciseGroup.VersionableExerciseGroup;
import com.hemerasolutions.v2trainer.model.library.Library;
import com.hemerasolutions.v2trainer.model.modality.Modality;
import com.hemerasolutions.v2trainer.model.modality.VersionableModality;
import com.hemerasolutions.v2trainer.model.training.TrainingType;
import com.hemerasolutions.v2trainer.service.interfaces.ExerciseGroupServiceInterface;


@Component("exerciseGroupSearchComp")
@Scope("prototype")
public class ExerciseGroupSearchComp extends BaseComponent implements ExerciseGroupSearchCompInterface{

	private static final long serialVersionUID = 1L;

	private ExerciseGroup selExerciseGroup;
	
	private VersionableExerciseGroup versionableExerciseGroup;
	
	private ExerciseGroup exerciseGroup;
	
	private ExerciseGroupSearchDelegateInterface delegate;
	
	private List<TrainingType> findExerciseGroupByTrainingTypes;

	private List<Modality> findExerciseGroupByModalitys;
	
	private String exerciseGroupFindByName;

	private String exerciseGroupFindByDescription;

	private String exerciseGroupFindByObs;

	private List<TrainingType> fullTrainingTypes;
	
	private List<Modality> fullModalitys;
	
	private Date exerciseGroupBegCreatedAt;
	
	private Date exerciseGroupEndCreatedAt;
	
	private Date exerciseGroupBegChangedAt;
	
	private Date exerciseGroupEndChangedAt;
	
	private boolean exerciseGroupFindByStatus;
	
	private List<VersionableExerciseGroup> foundedVersionableExerciseGroups;
	
	@Autowired
	private ExerciseGroupServiceInterface exerciseGroupService;
	
	@Autowired
	private ExerciseGroupDetailsCompInterface exerciseGroupDetails;
	
	@Override
	public void refresh() {
		this.fullTrainingTypes = exerciseGroupService.listTrainingType();
		this.exerciseGroupFindByStatus = true;
		this.fullModalitys = new ArrayList<Modality>();
	}
	
	@Override
	public void init(){
		super.init();
	}
	
	public void listAll(){
	   	Map<String,Object> fields = new HashMap<String, Object>();

       	fields.put("librarys", this.getActiveUserAllowedLibrarys());

        this.foundedVersionableExerciseGroups = this.exerciseGroupService.searchVersionableExerciseGroups(fields);
        
        if(this.delegate != null){
        	this.delegate.onVersionableModalitySearchCompleted(foundedVersionableExerciseGroups);
        }
	}
	
	@Override
	public void addExerciseGroup(){
		this.delegate.onVersionableExerciseGroupSelected(this.versionableExerciseGroup);
	}
	
	@Override
	public void toViewExerciseGroup(){
		this.exerciseGroupDetails.setExerciseGroup(this.exerciseGroup);
		this.exerciseGroupDetails.refresh();
	}
	
	@Override
	public void changeFullTrainingTypesAll(org.primefaces.event.ToggleSelectEvent ev){
		
		if(ev.isSelected()){
			this.findExerciseGroupByTrainingTypes = this.fullTrainingTypes;
		}else{
			this.findExerciseGroupByTrainingTypes = new ArrayList<TrainingType>();
		}
		
		this.changeFullTrainingTypes();
	}	
	

	@Override
	public void changeFullModalitysAll(org.primefaces.event.ToggleSelectEvent ev){
		
		if(ev.isSelected()){
			this.findExerciseGroupByModalitys = this.fullModalitys;
		}else{
			this.findExerciseGroupByModalitys = new ArrayList<Modality>();
		}
		
		this.changeFullTrainingTypes();
	}	
	
	
	@Override
	public void changeFullTrainingTypes(){
		this.fullModalitys = new ArrayList<Modality>();
		for(Library lib:this.getActiveUserAllowedLibrarys()){
			for(VersionableModality vMod:lib.getVersionableModalitys()){
				for(Modality mod:vMod.getModalityVersions()){
					if(this.findExerciseGroupByTrainingTypes.contains(mod.getTrainingType())){
						this.fullModalitys.add(mod);
					}
				}
			}
		}
	}
	
	@Override
	public void searchVersionableExerciseGroups(){
    	Map<String,Object> fields = new HashMap<String, Object>();
    	
		if( this.exerciseGroupBegCreatedAt != null && this.exerciseGroupEndCreatedAt != null && 
				this.exerciseGroupBegCreatedAt.compareTo(this.exerciseGroupEndCreatedAt) > 0){
				this.showErrorMessage(	this.localizedText("CreationDate"),
										this.localizedText("TheInitialDateNeedToBeGreaterThanFinal"));
				return;
			}
			

		if( this.exerciseGroupBegChangedAt != null && this.exerciseGroupEndChangedAt != null && 
			this.exerciseGroupBegChangedAt.compareTo(this.exerciseGroupEndChangedAt) > 0){
			this.showErrorMessage(	this.localizedText("ChangeDate"),
									this.localizedText("TheInitialDateNeedToBeGreaterThanFinal"));
			return;			
		}
    	
    	

    	if (!this.exerciseGroupFindByName.equals("")) {
            fields.put("name", this.exerciseGroupFindByName);
        }

    	if (!this.exerciseGroupFindByDescription.equals("")) {
            fields.put("description", this.exerciseGroupFindByDescription);
        }
    	
    	if (!this.exerciseGroupFindByObs.equals("")){
    		fields.put("obs",this.exerciseGroupFindByObs);
    	}
    	
    	if(this.findExerciseGroupByTrainingTypes != null && !this.findExerciseGroupByTrainingTypes.isEmpty() ){
    		fields.put("trainingTypes",this.findExerciseGroupByTrainingTypes);
    	}
    	
    	if(this.findExerciseGroupByModalitys != null && !this.findExerciseGroupByModalitys.isEmpty() ){
    		fields.put("modalitys",this.findExerciseGroupByModalitys);
    	}

    	if (this.exerciseGroupBegCreatedAt != null){
    		fields.put("begCreatedAt", this.exerciseGroupBegCreatedAt);
    	}
    	if (this.exerciseGroupEndCreatedAt != null){
            fields.put("endCreatedAt",this.exerciseGroupEndCreatedAt);
        }
        if (this.exerciseGroupBegChangedAt != null){
        	fields.put("begChangedAt",this.exerciseGroupBegChangedAt);
        }
        if (this.exerciseGroupEndChangedAt != null){
        	fields.put("endChangedAt", this.exerciseGroupEndChangedAt);
        }

        
        fields.put("librarys", this.getActiveUserAllowedLibrarys());
        
	    fields.put("active", this.exerciseGroupFindByStatus);
	    
	    this.foundedVersionableExerciseGroups = this.exerciseGroupService.searchVersionableExerciseGroups(fields);
	    
		if((this.foundedVersionableExerciseGroups).isEmpty()){
			this.showInfoMessage(	this.localizedText("SearchCompleted"),
									this.localizedText("NoEntrysFounded"));
		}else{
			this.showInfoMessage(	this.localizedText("SearchCompleted"),
									this.foundedVersionableExerciseGroups.size() + " " + 
									this.localizedText("entrysFounded"));
		}
	}

	@Override
	public List<TrainingType> getFindExerciseGroupByTrainingTypes() {
		return findExerciseGroupByTrainingTypes;
	}

	@Override
	public void setFindExerciseGroupByTrainingTypes(
			List<TrainingType> findExerciseGroupByTrainingTypes) {
		this.findExerciseGroupByTrainingTypes = findExerciseGroupByTrainingTypes;
	}

	@Override
	public List<Modality> getFindExerciseGroupByModalitys() {
		return findExerciseGroupByModalitys;
	}

	@Override
	public void setFindExerciseGroupByModalitys(
			List<Modality> findExerciseGroupByModalitys) {
		this.findExerciseGroupByModalitys = findExerciseGroupByModalitys;
	}

	@Override
	public String getExerciseGroupFindByName() {
		return exerciseGroupFindByName;
	}

	@Override
	public void setExerciseGroupFindByName(String exerciseGroupFindByName) {
		this.exerciseGroupFindByName = exerciseGroupFindByName;
	}

	@Override
	public String getExerciseGroupFindByDescription() {
		return exerciseGroupFindByDescription;
	}

	@Override
	public void setExerciseGroupFindByDescription(
			String exerciseGroupFindByDescription) {
		this.exerciseGroupFindByDescription = exerciseGroupFindByDescription;
	}

	@Override
	public String getExerciseGroupFindByObs() {
		return exerciseGroupFindByObs;
	}

	@Override
	public void setExerciseGroupFindByObs(String exerciseGroupFindByObs) {
		this.exerciseGroupFindByObs = exerciseGroupFindByObs;
	}

	@Override
	public List<TrainingType> getFullTrainingTypes() {
		return fullTrainingTypes;
	}

	@Override
	public void setFullTrainingTypes(List<TrainingType> fullTrainingTypes) {
		this.fullTrainingTypes = fullTrainingTypes;
	}

	@Override
	public List<Modality> getFullModalitys() {
		return fullModalitys;
	}

	@Override
	public void setFullModalitys(List<Modality> fullModalitys) {
		this.fullModalitys = fullModalitys;
	}

	@Override
	public Date getExerciseGroupBegCreatedAt() {
		return exerciseGroupBegCreatedAt;
	}

	@Override
	public void setExerciseGroupBegCreatedAt(Date exerciseGroupBegCreatedAt) {
		this.exerciseGroupBegCreatedAt = exerciseGroupBegCreatedAt;
	}

	@Override
	public Date getExerciseGroupEndCreatedAt() {
		return exerciseGroupEndCreatedAt;
	}

	@Override
	public void setExerciseGroupEndCreatedAt(Date exerciseGroupEndCreatedAt) {
		this.exerciseGroupEndCreatedAt = exerciseGroupEndCreatedAt;
	}

	@Override
	public Date getExerciseGroupBegChangedAt() {
		return exerciseGroupBegChangedAt;
	}

	@Override
	public void setExerciseGroupBegChangedAt(Date exerciseGroupBegChangedAt) {
		this.exerciseGroupBegChangedAt = exerciseGroupBegChangedAt;
	}

	@Override
	public Date getExerciseGroupEndChangedAt() {
		return exerciseGroupEndChangedAt;
	}

	@Override
	public void setExerciseGroupEndChangedAt(Date exerciseGroupEndChangedAt) {
		this.exerciseGroupEndChangedAt = exerciseGroupEndChangedAt;
	}

	@Override
	public boolean isExerciseGroupFindByStatus() {
		return exerciseGroupFindByStatus;
	}

	@Override
	public void setExerciseGroupFindByStatus(boolean exerciseGroupFindByStatus) {
		this.exerciseGroupFindByStatus = exerciseGroupFindByStatus;
	}


	@Override
	public List<VersionableExerciseGroup> getFoundedVersionableExerciseGroups() {
		return foundedVersionableExerciseGroups;
	}

	@Override
	public void setFoundedVersionableExerciseGroups(
			List<VersionableExerciseGroup> foundedVersionableExerciseGroups) {
		this.foundedVersionableExerciseGroups = foundedVersionableExerciseGroups;
	}

	@Override
	public ExerciseGroupServiceInterface getExerciseGroupService() {
		return exerciseGroupService;
	}

	@Override
	public void setExerciseGroupService(
			ExerciseGroupServiceInterface exerciseGroupService) {
		this.exerciseGroupService = exerciseGroupService;
	}


	@Override
	public ExerciseGroup getSelExerciseGroup() {
		return selExerciseGroup;
	}

	@Override
	public void setSelExerciseGroup(ExerciseGroup selExerciseGroup) {
		this.selExerciseGroup = selExerciseGroup;
	}

	@Override
	public ExerciseGroup getExerciseGroup() {
		return exerciseGroup;
	}

	@Override
	public void setExerciseGroup(ExerciseGroup exerciseGroup) {
		this.exerciseGroup = exerciseGroup;
	}

	@Override
	public ExerciseGroupDetailsCompInterface getExerciseGroupDetails() {
		return exerciseGroupDetails;
	}

	@Override
	public void setExerciseGroupDetails(
			ExerciseGroupDetailsCompInterface exerciseGroupDetails) {
		this.exerciseGroupDetails = exerciseGroupDetails;
	}

	@Override
	public ExerciseGroupSearchDelegateInterface getExerciseGroupSearchDelegate() {
		return delegate;
	}

	@Override
	public void setExerciseGroupSearchDelegate(
			ExerciseGroupSearchDelegateInterface exerciseGroupSearchDelegate) {
		this.delegate = exerciseGroupSearchDelegate;
	}

	@Override
	public ExerciseGroupSearchDelegateInterface getDelegate() {
		return delegate;
	}

	@Override
	public void setDelegate(ExerciseGroupSearchDelegateInterface delegate) {
		this.delegate = delegate;
	}

	@Override
	public VersionableExerciseGroup getVersionableExerciseGroup() {
		return versionableExerciseGroup;
	}

	@Override
	public void setVersionableExerciseGroup(
			VersionableExerciseGroup versionableExerciseGroup) {
		this.versionableExerciseGroup = versionableExerciseGroup;
	}

	
	
}
